using System;
using System.Diagnostics;
using System.Windows.Forms;
using CastleSample.Business.Client;
using CastleSample.Business.Interface;
using CastleSample.Entity;
using CastleSample.Exceptions;
using CastleSample.UI.Core;

namespace CastleSample.Modules.Order
{
    public partial class FormListAllOrders : Form, IToolStripForm
    {
        private bool m_IsDirty = false;
        private OrderEntityCollection m_Orders;

        public OrderEntityCollection Orders
        {
            get { return m_Orders; }
            set { m_Orders = value; }
        }

        private IOrderBizService m_OrderBizService;

        public IOrderBizService OrderBizService
        {
            get { return m_OrderBizService; }
            set { m_OrderBizService = value; }
        }

        public ToolStrip MainToolStrip
        {
            get { return toolStripMain; }
        }

        public FormListAllOrders()
        {
            m_OrderBizService = BizServiceFactory.GetService<IOrderBizService>();
            InitializeComponent();
        }

        private void FormListAllOrders_Load(object sender, EventArgs e)
        {
            ApplicationContainer.Instance.EventBroker.RegisterAsyncEventHandler("OrderCreated",
                OrderCreated_EventHandler);
            m_Orders = OrderBizService.FindAllOrders();
            dataGridViewOrders.DataSource = m_Orders;
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = dataGridViewOrders.CurrentRow;
            Debug.Assert(currentRow != null);
            OrderEntity currentOrder = currentRow.DataBoundItem as OrderEntity;
            Debug.Assert(currentOrder != null);
            OrderBizService.DeleteOrder(currentOrder.OrderId);
            m_Orders = OrderBizService.FindAllOrders();
            dataGridViewOrders.DataSource = m_Orders;
        }

        private void dataGridViewOrders_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            toolStripButtonDelete.Enabled = true;
        }

        private void dataGridViewOrders_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            toolStripButtonDelete.Enabled = true;
        }

        private void toolStripButtonView_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = dataGridViewOrders.CurrentRow;
            Debug.Assert(currentRow != null);
            OrderEntity currentOrder = currentRow.DataBoundItem as OrderEntity;
            Debug.Assert(currentOrder != null);
            ApplicationContainer.Instance.EventBroker.PublishEvent("ViewOrder", this,
                new ContainerEventArgs("OrderId", currentOrder.OrderId));
        }

        private void OrderCreated_EventHandler(object sender, ContainerEventArgs args)
        {
            m_Orders = OrderBizService.FindAllOrders();
            m_IsDirty = true;
        }

        private void FormListAllOrders_Activated(object sender, EventArgs e)
        {
            if (m_IsDirty)
            {
                dataGridViewOrders.DataSource = m_Orders;
            }
        }

        private void toolStripButtonUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = dataGridViewOrders.CurrentRow;
            Debug.Assert(currentRow != null);
            OrderEntity currentOrder = currentRow.DataBoundItem as OrderEntity;
            Debug.Assert(currentOrder != null);
            currentOrder.Status = OrderStatus.Deliveried;
            try
            {
                OrderBizService.UpdateOrder(currentOrder, false);
            }
            catch (VersionConflictException ex)
            {
                DialogResult ret =
                    MessageBox.Show(this,
                        "Other user had updated the order after you load it, do you want force update it?",
                        "CastleSample", MessageBoxButtons.YesNo);
                if (ret == DialogResult.Yes)
                {
                    OrderBizService.UpdateOrder(currentOrder, true);
                }
                else
                {
                    m_Orders = OrderBizService.FindAllOrders();
                    dataGridViewOrders.DataSource = m_Orders;
                }
            }
        }
    }
}